#pragma once

#include "SingletonThreadPool.hpp"
#include "init.hpp"
#include "log.hpp"

Init init;

enum
{
    EXITCODE = 0,
    DIVZERO,
    MODZERO
};

class Task
{
public:
    Task(int sockfd, const uint16_t &port, const std::string &ip): _sockfd(sockfd), _clientport(port), _clientip(ip) 
    {}

    void operator()()
    {
        run();
    }

    void run()
    {
		char buffer[4096];

		// std::cout << "开始接收消息"<< std::endl;
		ssize_t n = read(_sockfd, buffer, sizeof(buffer) - 1);
		if (n > 0)
		{
			buffer[n] = {0};
			std::cout << "client say@ ";
			// std::string message = buffer;

			std::string message = init.translation(buffer);
			std::cout << message << std::endl;
			write(_sockfd, message.c_str(), sizeof(message));
		}
		else if (n == 0)
		{
			lg(INFO, "Did not get it! [sockfd: %d, clientip: %s : clientport: %d] quit...\n", _sockfd, _clientip, _clientport);
		}
		else
		{
			lg(WARNING, "Did not get it! [sockfd: %d, clientip: %s : clientport: %d] quit...\n", _sockfd, _clientip, _clientport);
		}

		close(_sockfd);
	}

    ~Task()
    {}

private:
   int _sockfd;
   uint16_t _clientport;
   std::string _clientip;
};
